﻿/*
 * This software is the confidential and proprietary
 * information of NetBay
 * Such confidential information should not be disclosed
 * NetBay copyright(c) 2010
 */

#region Using
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using NetBay.Core.BusinessEntities;
using System.ServiceModel.DomainServices.Server;
using NetBay.Core.Extensions;
using Rio.Applications.Web.Services.BusinessEntities;
using Rio.Doc.Business.Repository;
using NetBay.Core.BusinessEntities.Historic;
#endregion

namespace Rio.Applications.Web.Services
{

    public partial class BusinessEntitiesDomainService : DomainService
    {


        /// <summary>
        /// Add a user's action
        /// </summary>
        /// <param name="p_iUserActions">the user's action</param>
        /// <param name="p_lFolderTypeId">the folder type identifier</param>
        /// <param name="p_lFolderId">the flder Identifier</param>
        /// <param name="p_sDocumentId">the document Identifier</param>
        /// <param name="p_sFileName">The original filename</param>
        /// <param name="p_iCpt">p_iCpt</param>
        /// <param name="p_sIndexes">p_sIndexes</param>
        /// <param name="p_bSauve">p_bSauve</param>
        /// <returns>The identifier of the message flow</returns>
        /// <remarks>User do not know if this operation fails or not (explicit request)</remarks>
        [Query(HasSideEffects = true)]
        public IEnumerable<ReturnObject> AddHistoricInfoConsult(int p_iUserActions, long p_lFolderTypeId, long p_lFolderId, string p_sFolderIdDoc = "", string p_sDocumentId = "" , string p_sFileName= "" , string p_sIndexes = "",bool p_bSave = false, int p_iNbDoc = 0, int p_iNbResult=0, int p_iNbIndexed=0, string p_sIndexName ="")

        {
            List<ReturnObject> l_oRet = new List<ReturnObject>();

            Constant.UserActions l_iUserAction = Constant.UserActions.None;
            try
            {
                l_iUserAction = (Constant.UserActions)Enum.Parse(typeof(Constant.UserActions), p_iUserActions.ToString());
                switch (l_iUserAction)
                {
                    case Constant.UserActions.CONSULTATION_DOSSIER:
                        UserRepository.AddUserAction(GetUser().UserId, l_iUserAction.szGetDescription(), p_lFolderTypeId, p_lFolderId, null, null, p_sIndexes, p_sFolderIdDoc, p_iNbDoc);
                        break;
                    case Constant.UserActions.RECHERCHE:
                        UserRepository.AddUserAction(GetUser().UserId, l_iUserAction.szGetDescription(), p_lFolderTypeId, p_lFolderId, null, null, p_sIndexes, null, 0, p_bSave,p_iNbResult);
                        break;
                    case Constant.UserActions.AJOUT_DOCUMENTS_DOSSIER:
                        UserRepository.AddUserAction(GetUser().UserId, l_iUserAction.szGetDescription(), p_lFolderTypeId, p_lFolderId, p_sDocumentId, p_sFileName, p_sIndexes, null, 0, p_bSave, 0, p_iNbIndexed, p_sIndexName);
                    
                        break;
                }
                l_oRet.Add(new ReturnObject());
            }
            catch (Exception ex)
            {
                ReturnObject l_oReturnObject = new ReturnObject("(p_sAction<" + l_iUserAction.szGetDescription() + "> p_lFolderTypeId<" + p_lFolderTypeId + "> p_lFolderId<" + p_lFolderId + "> p_sFolderIdDoc<" + p_sFolderIdDoc + "> p_sDocumentId<" + p_sDocumentId + "> p_sFileName<" + p_sFileName + "> p_sIndexes<" + p_sIndexes + "> p_bSave<" + p_bSave + "> p_iNbDoc<" + p_iNbDoc + "> p_iNbResult<" + p_iNbResult + "> p_iNbIndexed<" + p_iNbIndexed + "> p_sIndexName<" + p_sIndexes + "> )...", ex);
                NetBay.Core.Log.LogManager.Instance().LogError(l_oReturnObject.FullErrorMessage);
                l_oRet.Add(l_oReturnObject);
            }

            return l_oRet;
        }

        /// <summary>
        /// Gets the last consultations.
        /// </summary>
        /// <param name="p_sAction">The action.</param>
        /// <param name="p_lUserId">The user id.</param>
        /// <param name="p_iNbLine">The nb line.</param>
        /// <param name="p_bSearch">The search.</param>
        /// <returns></returns>
        [Query(HasSideEffects = true)]
        public List<HistoricInformation> GetLastConsultations(string p_sAction, long p_lUserId, int p_iNbLine, bool p_bSearch)
        {
            var l_oHistoricInfoResults = new List<HistoricInformation>();

            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("(p_lUserId<" + p_lUserId + "> p_iNbLine<" + p_iNbLine + ">p_iNbLine<" + p_iNbLine + ">p_bSearch<" + p_bSearch + ">)...");
                l_oHistoricInfoResults = HistoricInfoRepository.GetLastConsultations(p_sAction, p_lUserId, p_iNbLine, p_bSearch);
                NetBay.Core.Log.LogManager.Instance().LogDebug(l_oHistoricInfoResults.Count + " results...");
            }
            catch (Exception ex)
            {
                var l_oHistoricInfoResult = new HistoricInformation();
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in Historic Consultation : " + ex.Message);
                l_oHistoricInfoResults.Add(l_oHistoricInfoResult);
            }
            return l_oHistoricInfoResults;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="p_lIdentifier"></param>
         [Invoke(HasSideEffects = true)]
        public void DeleteHistoricSearcSave(long p_lIdentifier)
        {
            
            try
            {
                NetBay.Core.Log.LogManager.Instance().LogDebug("HistoricInfoDS.DeleteHistoricSearcSave:");
                HistoricInfoRepository.DeleteHistoricSearcSave(p_lIdentifier);
            }
            catch (Exception ex)
            {
                NetBay.Core.Log.LogManager.Instance().LogError("Exception in HistoricInfoDS.DeleteHistoricSearcSave: " + ex.Message);
            }

        }

    }
}